package org.theSeed.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.theSeed.base.pojo.vo.R;
import org.theSeed.fileContract.pojo.vo.CancleChipFileProxyVo;
import org.theSeed.fileContract.pojo.vo.SeedCheckChipFileProxyVo;
import org.theSeed.fileContract.pojo.vo.SeedUploadChipFileInitProxyVo;
import org.theSeed.fileContract.pojo.vo.SeedUploadFileProxyVo;
import org.theSeed.pojo.ro.*;
import org.theSeed.service.FileService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     */
    @GetMapping("getFile")
    public void getFile(@RequestParam("resourceUrl") String resourceUrl, HttpServletResponse resp) {
        fileService.getFile(resourceUrl,resp);
    }

    /**
     * 文件上传
     */
    @PostMapping("uploadFile")
    public R<SeedUploadFileProxyVo> uploadFile(@RequestParam("file") MultipartFile file) {
        return R.success(fileService.uploadFile(file));
    }

    /**
     * 文件更新
     */
    @PostMapping("updateFile")
    public R<SeedUploadFileProxyVo> updateFile(@Valid UpdateFileRo updateFileRo) {
        return R.success(fileService.updateFile(updateFileRo));
    }

    /**
     * 文件删除
     */
    @PostMapping("deleteFile")
    public R<SeedUploadFileProxyVo> deleteFile(@RequestBody @Valid ResourceBo resourceBo) {
        return R.success(fileService.deleteFile(resourceBo));
    }

    /**
     * 分片上传初始化
     */
    @PostMapping("initUploadChipFile")
    public R<SeedUploadChipFileInitProxyVo> initUploadChipFile(@RequestBody @Valid UploadChipFileInitRo uploadChipFileInitRo) {
        return R.success(fileService.initUploadChipFile(uploadChipFileInitRo));
    }

    /**
     * 分片上传
     */
    @PostMapping("uploadChipFile")
    public R<SeedUploadFileProxyVo> uploadChipFile(@Valid UploadChipFileRo uploadChipFileRo) {
        return R.success(fileService.uploadChipFile(uploadChipFileRo));
    }

    /**
     * 文件合并
     */
    @PostMapping("mergeChipFile")
    public R<SeedUploadFileProxyVo> mergeChipFile(@RequestBody @Valid MergeChipFileRo mergeChipFileRo) {
        return R.success(fileService.mergeChipFile(mergeChipFileRo));
    }

    /**
     * 校验分片文件
     */
    @PostMapping("checkChipFile")
    public R<SeedCheckChipFileProxyVo> checkChipFile(@RequestBody @Valid CheckChipFileRo checkChipFileRo) {
        return R.success(fileService.checkChipFile(checkChipFileRo));
    }

    /**
     * 取消分片上传
     */
    @PostMapping("cancleChipFile")
    public R<CancleChipFileProxyVo> cancleChipFile(@RequestBody @Valid CancleChipFileRo cancleChipFileRo) {
        return R.success(fileService.cancleChipFile(cancleChipFileRo));
    }
}
